package com.hb.trans.mvp.presenter

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.OnLifecycleEvent
import android.content.DialogInterface
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import com.basiclib.base.BasePresenter
import com.basiclib.lifecycle.RxLifecycleUtils
import com.basiclib.network.RetryWithDelay
import com.basiclib.network.RxErrorHandler
import com.basiclib.widget.OnDialogDismissListener
import com.hb.trans.bean.Base
import com.hb.trans.bean.Student
import com.hb.trans.global.DataStore
import com.hb.trans.global.ErrorHandleSubscriber
import com.hb.trans.mvp.contract.TransCheckLoginContract
import com.hb.trans.mvp.model.TransCheckLoginModel
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.util.*
import javax.inject.Inject

class TransCheckLoginPresenter @Inject constructor(model: TransCheckLoginModel, var errorHandler: RxErrorHandler) :
    BasePresenter<TransCheckLoginContract.View, TransCheckLoginContract.Model>(model), OnDialogDismissListener {


    private var loginDisposable: Disposable? = null
    private lateinit var uuid: String

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    fun loadSecurity() {
        uuid = UUID.randomUUID().toString()
        mModel.securityCode(uuid)
            .subscribeOn(Schedulers.io())
            .retryWhen(RetryWithDelay())
            .observeOn(AndroidSchedulers.mainThread())
            .compose(RxLifecycleUtils.bindToLifecycle(mRootView!!))
            .map { it.byteStream() }
            .map { BitmapFactory.decodeStream(it) }
            .subscribe(object : ErrorHandleSubscriber<Bitmap>(errorHandler) {
                override fun next(t: Bitmap) {
                    mRootView?.setSecurityImage(t)
                }
            })
    }

    fun login(phone: String, cardCode: String, carType: String, verifyCode: String) {
        mModel.login(phone, cardCode, carType, verifyCode, uuid)
            .subscribeOn(Schedulers.io())
            .retryWhen(RetryWithDelay())
            .doOnSubscribe { mRootView?.showLoading("登录中...", listener = this@TransCheckLoginPresenter) }
            .observeOn(AndroidSchedulers.mainThread())
            .compose(RxLifecycleUtils.bindToLifecycle(mRootView!!))
            .subscribe(object : ErrorHandleSubscriber<Base<Student>>(errorHandler) {
                override fun onSubscribe(d: Disposable) {
                    loginDisposable = d
                }

                override fun next(re: Base<Student>) {
                    if (re.code == 200) {
                        re.data?.let {
                            DataStore.getInstance().saveStudentInfo(it)
                            mRootView?.loginSucess()
                        }
                    } else {
                        mRootView?.showShortToast(re.message ?: "")
                    }
                }
            })
    }

    override fun onDismiss(dialog: DialogInterface?, status: Int, tag: Int) {

    }
}